import { HttpClient } from '@angular/common/http';
import { Inject, Injectable, Injector } from '@angular/core';
import { Observable } from 'rxjs';
import { Banner, HotTag, SongSheet } from './data-types/common.types';
import { API_CONFIG, ServicesModule } from './services.module';
import { map } from 'rxjs/internal/operators';

@Injectable({
  providedIn: ServicesModule
})
export class HomeService {

  constructor(private http:HttpClient, @Inject(API_CONFIG) private uri:string ) { }
  
  getBanners(): Observable<Banner[]>{
    return this.http.get(this.uri + 'banner')
    .pipe(map((res:any)=>res.banners));
  }
  getHotTags(): Observable<HotTag[]>{
    return this.http.get(this.uri + 'playlist/hot')
    .pipe(map((res:any)=>{
      //排序获得前五位的数据
      return res.tags.sort((x:HotTag,y:HotTag)=>{
        return x.position - y.position;
      }).slice(0,5)
    }));
  }
  getPerosonalSheetList(): Observable<SongSheet[]>{
    return this.http.get(this.uri + 'personalized')
    .pipe(map((res:any)=>res.result.slice(0,16)));
  }
}
